package com.timk.goserver.client.model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.user.client.rpc.IsSerializable;

/** Game invite */
public class GameInvite implements IsSerializable {
	private int inviteId;
	private List playerInfo;
	private int size;
	private boolean properHandicap;
	private int handicap;
	private double komi;
	private boolean rated;
	private String comment;
	
	/** */
	public GameInvite() {
		size = 19;
		properHandicap = true;
		komi = 6.5;
		rated = true;
	}
	
	/**
	 * Replaces hibernate list classes if necessary, so that
	 * this object can be serialized.
	 *
	 */
	public void sanitize() {
		if(!(playerInfo instanceof ArrayList)) {
			playerInfo = new ArrayList(playerInfo);
		}
	}
	
	/**
	 * Checks to see if a given player is in this invite
	 * @param player
	 * @return true if found
	 */
	public boolean findPlayer(String player) {
		for (Iterator iter = playerInfo.iterator(); iter.hasNext();) {
			InvitePlayerInfo info = (InvitePlayerInfo) iter.next();
			if(player.equals(info.getName())) {
				return true;
			}
		}
		
		return false;
	}
	
	/**
	 * Accepts the invite on behalf of a specific player
	 * @param player
	 * @return true if the player was found
	 */
	public boolean setPlayerAccepted(String player) {
		for (int i = 0; i < playerInfo.size(); i++) {
			InvitePlayerInfo info = (InvitePlayerInfo) playerInfo.get(i);
			if(player.equals(info.getName())) {
				info.setAccepted(true);
				return true;
			}
		}
		
		return false;
	}
	
	/**
	 * Returns true if the player has accepted this invite
	 * @param player
	 * @return true if the player has accepted this invite
	 */
	public boolean hasPlayerAccepted(String player) {
		for(int i = 0; i < playerInfo.size(); i++) {
			InvitePlayerInfo info = (InvitePlayerInfo) playerInfo.get(i);
			if(player.equals(info.getName())) {
				return info.isAccepted();
			}
		}
		
		return false;
	}
	
	/**
	 * Returns true if all players have accepted the invite
	 * @return true if all players have accepted the invite
	 */
	public boolean allPlayersAccepted() {
		for (Iterator iter = playerInfo.iterator(); iter.hasNext();) {
			InvitePlayerInfo info = (InvitePlayerInfo) iter.next();
			if(!info.isAccepted()) {
				return false;
			}
		}
		
		return true;
	}
	
	/**
	 * Returns the board size
	 * @return the board size
	 */
	public int getSize() {
		return size;
	}
	
	/**
	 * Sets the board size
	 * @param size the size
	 */
	public void setSize(int size) {
		this.size= size;
	}
	
	/**
	 * Returns the comment
	 * @return the comment
	 */
	public String getComment() {
		return comment;
	}
	
	/**
	 * Sets the comment
	 * @param comment
	 */
	public void setComment(String comment) {
		this.comment = comment;
	}
	
	/**
	 * Returns the handicap
	 * @return the handicap
	 */
	public int getHandicap() {
		return handicap;
	}
	
	/**
	 * Sets the handicap
	 * @param handicap
	 */
	public void setHandicap(int handicap) {
		this.handicap = handicap;
	}
	
	/**
	 * Returns the komi
	 * @return the komi
	 */
	public double getKomi() {
		return komi;
	}
	
	/**
	 * Sets the komi
	 * @param komi
	 */
	public void setKomi(double komi) {
		this.komi = komi;
	}
	
	/**
	 * Returns the invite id
	 * @return the id
	 */
	public int getInviteId() {
		return inviteId;
	}
	
	/**
	 * Sets the invite id
	 * @param inviteId
	 */
	public void setInviteId(int inviteId) {
		this.inviteId = inviteId;
	}

	/**
	 * Returns the players' names
	 * @return List&lt;String&gt;
	 */
	public List getPlayerNames() {
		List names = new ArrayList();
		for (Iterator iter = playerInfo.iterator(); iter.hasNext();) {
			InvitePlayerInfo info = (InvitePlayerInfo) iter.next();
			names.add(info.getName());
		}
		
		return names;
	}

	/**
	 * Sets the players
	 * @param players
	 */
	public void setPlayers(List players) {
		playerInfo = new ArrayList();
		for (Iterator iter = players.iterator(); iter.hasNext();) {
			String name = (String) iter.next();
			playerInfo.add(new InvitePlayerInfo(name));
		}
	}

	/**
	 * Returns true if rated
	 * @return true if rated
	 */
	public boolean isRated() {
		return rated;
	}

	/**
	 * Sets whether the game is rated
	 * @param rated
	 */
	public void setRated(boolean rated) {
		this.rated = rated;
	}

	/**
	 * Returns true if using proper handicap
	 * @return true if using proper handicap
	 */
	public boolean isProperHandicap() {
		return properHandicap;
	}

	/**
	 * Sets proper handicap
	 * @param properHandicap true if using proper handicap
	 */
	public void setProperHandicap(boolean properHandicap) {
		this.properHandicap = properHandicap;
	}

	/** Just for hibernate/serialization */
	List getPlayerInfo() {
		return playerInfo;
	}

	/** Just for hibernate/serialization */
	void setPlayerInfo(List playerInfo) {
		this.playerInfo = playerInfo;
	}
}
